import sys
sys.path.append('.')
from app.core.config import settings
from pymongo import MongoClient

try:
    print("开始连接数据库...")
    client = MongoClient(settings.MONGODB_URL)
    db = client[settings.MONGODB_DATABASE]
    users_collection = db['users']
    
    print('数据库中所有用户的角色信息:')
    users = list(users_collection.find({}, {'username': 1, 'roles': 1, '_id': 0}))
    
    all_roles = set()
    for user in users:
        if 'roles' in user and user['roles']:
            for role in user['roles']:
                all_roles.add(role)
        print(f'用户: {user.get("username", "未知")}, 角色: {user.get("roles", [])}')
    
    print(f'\n数据库中实际存在的所有角色类型: {sorted(list(all_roles))}')
    
    client.close()
    print("数据库连接已关闭")
    
except Exception as e:
    print(f'错误: {e}')
    import traceback
    traceback.print_exc()

print("脚本执行完成")